package com.platform.modules.wallet.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.web.service.impl.BaseServiceImpl;
import com.platform.modules.wallet.dao.WalletAccountDao;
import com.platform.modules.wallet.domain.WalletAccount;
import com.platform.modules.wallet.enums.AccountStatusEnum;
import com.platform.modules.wallet.service.WalletAccountService;
import com.platform.modules.wallet.vo.AccountVo01;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

@Slf4j
@Service("chatAccountService")
public class WalletAccountServiceImpl extends BaseServiceImpl<WalletAccount> implements WalletAccountService {

    @Resource
    private WalletAccountDao walletAccountDao;

    @Autowired
    public void setBaseDao() {
        super.setBaseDao(walletAccountDao);
    }


    @Override
    public WalletAccount createRecord(AccountVo01 accountVo) {
        return createRecord(accountVo, false);
    }

    @Override
    public WalletAccount createRecord(AccountVo01 accountVo, boolean negative) {

        WalletAccount bean = getBean();
        bean.setOtherId(accountVo.getOtherId());

        if (negative) {//是否负数
            bean.setAmount(-accountVo.getAmount());
        } else {
            bean.setAmount(accountVo.getAmount());
        }

        bean.setType(accountVo.getType());
        bean.setUserId(accountVo.getUserId());
        this.add(bean);
        return bean;
    }

    @Override
    public void successRecord(Long id) {
        WalletAccount account = findById(id);
        account.setStatus(AccountStatusEnum.SUCCESS.getCode());
        account.setSuccessTime(DateUtil.date());
        updateById(account);
    }

    @Override
    public IPage<WalletAccount> queryByUserId(Long userId) {

        IPage<WalletAccount> page = new Page(1, 20);//参数一是当前页，参数二是每页个数

        page = new WalletAccount().selectPage(page,
                new LambdaQueryWrapper<WalletAccount>()
                        .eq(WalletAccount::getUserId, userId)
                        .orderByDesc(WalletAccount::getId));
        return page;
    }

    @Override
    public List<WalletAccount> queryYesterday(Long userId) {

        Date date = DateUtil.date();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, -1);//把日期往后增加一天.整数往后推,负数往前移动
        date = DateUtil.date(calendar.getTime()); //这个时间就是日期往后推一天的结果

        String format = DateUtil.format(date, "yyyy-MM-dd");

        return new WalletAccount().selectList(
                new LambdaQueryWrapper<WalletAccount>()
                .eq(WalletAccount::getUserId,userId)
                        .eq(WalletAccount::getCreateDate,format));
    }

    @Override
    public List<WalletAccount> queryToday(Long userId) {

        Date date = DateUtil.date();
        String format = DateUtil.format(date, "yyyy-MM-dd");

        return new WalletAccount().selectList(
                new LambdaQueryWrapper<WalletAccount>()
                        .eq(WalletAccount::getUserId,userId)
                        .eq(WalletAccount::getCreateDate,format));
    }

    private WalletAccount getBean() {
        WalletAccount account = new WalletAccount();
        account.setStatus(AccountStatusEnum.INIT.getCode());
        account.setCreateTime(DateUtil.date());
        account.setCreateDate(DateUtil.date());
        return account;
    }
}
